#include "buffer.h"
#include "qlog.h"
#include <stdio.h>
#include <unistd.h>
static void put(char c)
{
  buffer_put(buffer_2,&c,1);
}

static void hex(unsigned char c)
{
  put("0123456789abcdef"[(c >> 4) & 15]);
  put("0123456789abcdef"[c & 15]);
}

static void octal(unsigned char c)
{
  put('\\');
  put('0' + ((c >> 6) & 7));
  put('0' + ((c >> 3) & 7));
  put('0' + (c & 7));
}

char * fqdn_read (char *out, int *label_idx, int *len, const char *q)
{
	char ch;
	int maxlen = *len;
	char ch2;
	int cur_len = 0;
	register int i = 0;
	//int last = -1;
	register int j = 0;
  if (!*q) {
		out[0] = '.';
		label_idx[0] = -1;
		*len = 1;
		return out;
  } else {
		label_idx[0] = 0;
    for (;;) {
      ch = *q++;
			cur_len+=ch;
			if (cur_len >= maxlen) break;
      while (ch--) {
        ch2 = *q++;
        if ((ch2 >= 'A') && (ch2 <= 'Z'))
	  			ch2 += 32;
        if (((ch2 >= 'a') && (ch2 <= 'z')) || ((ch2 >= '0') && (ch2 <= '9')) || (ch2 == '-') || (ch2 == '_')) {
					out[j++] = ch2;
        } else {
					out[j] = '\\';j++;
					out[j+1] = '0' + ((ch2 >> 6) & 7);
					out[j+2] = '0' + ((ch2 >> 3) & 7);
					out[j+3] = '0' + (ch2 & 7);
					j+=4;
				}
      }
      if (!*q) break;
			out[j++] = '.';
			i+=1;
			if (i == 32) break;
			label_idx[i] = j;
			cur_len+=1;
    }
	}
	out[j]='\0';
	*len = cur_len;
	return out;
}

void dlog(const char *q)
{
	char ch;
	char ch2;
  if (!*q)
    put('.');
  else
    for (;;) {
      ch = *q++;
      while (ch--) {
        ch2 = *q++;
        if ((ch2 >= 'A') && (ch2 <= 'Z'))
	  			ch2 += 32;
        if (((ch2 >= 'a') && (ch2 <= 'z')) || ((ch2 >= '0') && (ch2 <= '9')) || (ch2 == '-') || (ch2 == '_'))
	  			put(ch2);
        else
	  			octal(ch2);
      }
      if (!*q) break;
      put('.');
    }
  put('\n');
  buffer_flush(buffer_2);
}

void qlog(const char ip[4],uint16 port,const char id[2],const char *q,const char qtype[2],const char *result)
{
  char ch;
  char ch2;

  hex(ip[0]);
  hex(ip[1]);
  hex(ip[2]);
  hex(ip[3]);
  put(':');
  hex(port >> 8);
  hex(port & 255);
  put(':');
  hex(id[0]);
  hex(id[1]);
  buffer_puts(buffer_2,result);
  hex(qtype[0]);
  hex(qtype[1]);
  put(' ');

  if (!*q)
    put('.');
  else
    for (;;) {
      ch = *q++;
      while (ch--) {
        ch2 = *q++;
        if ((ch2 >= 'A') && (ch2 <= 'Z'))
	  ch2 += 32;
        if (((ch2 >= 'a') && (ch2 <= 'z')) || ((ch2 >= '0') && (ch2 <= '9')) || (ch2 == '-') || (ch2 == '_'))
	  put(ch2);
        else
	  octal(ch2);
      }
      if (!*q) break;
      put('.');
    }

  put('\n');
  buffer_flush(buffer_2);
}
